package com.alan.derbydemo.utils;

import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author alan
 * @description:
 * @date 2021/4/28
 */
@Component
public class DerbyUtils {
    public void executeSql(Connection cn, String sql){
        if (sql.endsWith(";"))
            sql = sql.substring(0, sql.length() - 1);

        Statement st = null;
        try{
            st = cn.createStatement();

            boolean query = st.execute(sql);

            System.out.println("-----------------------------------------");
            if (!query){
                System.out.println(st.getUpdateCount() + " records affected.");
            }
            System.out.println("-----------------------------------------");
        }
        catch (Throwable t){
            t.printStackTrace();
        }
        finally{
            try {
                assert st != null;
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
